function fitness_for_ga = calculateFitness(chromosome, DSM)
    
    originalDSM = DSM;
    n = size(chromosome,1); % 模块数
    fi = 0.5; 

    % 尽早检查空的或无效的染色体
     if isempty(chromosome) || n == 0 ...
       || any(sum(chromosome, 1) ~= 1) ...     % 每个组件必须恰好属于一个模块
       || any(sum(chromosome, 2) == 0)         % 不允许空模块
        objective_value = 1e7;  % 高额惩罚
        fitness_for_ga = -objective_value;     % 适用于 GA 的最大化适应度
        return;
    end
    M_elements = size(chromosome, 2); % Total number of elements
    if M_elements ~= size(DSM,1)
        %warning('DSM size does not match chromosome element count in calculateFitness.'); % Can be noisy
        objective_value = 1e7; % High penalty
        fitness_for_ga = -objective_value;
        return;
    end

    num = zeros(1,n);
    for i=1:n
        num(i) = sum(chromosome(i,:));
    end

    DSM_blocks = cell(1,n);
    index = cell(1,n);
    for i=1:n
        if num(i) > 0
            DSM_blocks{i} = zeros(num(i), num(i));
            index{i} = find(chromosome(i,:));
            % Ensure indices are within bounds of originalDSM
            if any(index{i} > size(originalDSM,1)) || any(index{i} < 1)
                 %warning('Invalid index found when creating DSM_blocks.'); % Can be noisy
                 objective_value = 1e7; % Penalize
                 fitness_for_ga = -objective_value;
                 return;
            end
            for ii=1:num(i)
                for jj=1:num(i)
                    DSM_blocks{i}(ii,jj) = originalDSM(index{i}(ii), index{i}(jj));
                end
            end
        else
            DSM_blocks{i} = zeros(0,0); % Represents an empty matrix
            index{i} = [];
        end
    end

    % Complexity calculation
    alpha = cell(1,n+1);
    for i=1:n
        alpha{i} = ones(1, num(i)); 
    end
    alpha{n+1} = zeros(1,n);

    for i=1:n
        if num(i) > 0
            C1_temp = individual_Complexity(alpha{i});
            C2_temp = interaction_Complexity(DSM_blocks{i}, fi);
            C3_temp = topological_Complexity(DSM_blocks{i});
            C_total_temp = [C1_temp C2_temp C3_temp]; 
            alpha{n+1}(i) = total_Complexity(C_total_temp);
        else
            alpha{n+1}(i) = 0;
        end
    end

    DSM_new = zeros(n,n);
    for i=1:n
        for j=1:n
            if i ~= j && ~isempty(index{i}) && ~isempty(index{j})
                if any(index{i} > size(originalDSM,1)) || any(index{j} > size(originalDSM,1)) || ...
                   any(index{i} < 1) || any(index{j} < 1)
                     %warning('Invalid index found when creating DSM_new.'); % Can be noisy
                     objective_value = 1e7; 
                     fitness_for_ga = -objective_value;
                     return;
                end
                DSM_new(i,j) = sum(sum(originalDSM(index{i}, index{j})));
            end
        end
    end

    objective_value = 0; 
    try
        C1_new = individual_Complexity(alpha{n+1});
        C2_new = interaction_Complexity(DSM_new, fi);
        C3_new = topological_Complexity(DSM_new);
        C_total_new = [C1_new C2_new C3_new]; 
        C_new = total_Complexity(C_total_new);

        if isnan(C_new) || ~isreal(C_new)
            objective_value = 1e6;
        else
            objective_value = C_new;
        end
    catch ME_calc
        %fprintf('Error during final complexity calculation: %s\n', ME_calc.message); % Can be noisy
        objective_value = 1e7;
    end
    % --- End of your provided objective function code ---

    fitness_for_ga = -objective_value; % GA maximizes this, so it minimizes objective_value

end